1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_ANY_NULL_QUERIES;
22  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
23  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24  import static com.google.common.truth.Truth.assertThat;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.collect.Multimap;
28  import com.google.common.collect.testing.Helpers;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import com.google.common.collect.testing.features.MapFeature;
31  
32  import java.util.Collection;
33  
34  /**
35   * Tests for {@link Multimap#removeAll(Object)}.
36   *
37   * @author Louis Wasserman
38   */
39  @GwtCompatible
40  public class MultimapRemoveAllTester<K, V> extends AbstractMultimapTester<K, V, Multimap<K, V>> {
41    @MapFeature.Require(SUPPORTS_REMOVE)
42    public void testRemoveAllAbsentKey() {
43      assertThat(multimap().removeAll(sampleKeys().e3)).isEmpty();
44      expectUnchanged();
45    }
46  
47    @CollectionSize.Require(absent = ZERO)
48    @MapFeature.Require(SUPPORTS_REMOVE)
49    public void testRemoveAllPresentKey() {
50      assertThat(multimap().removeAll(sampleKeys().e0))
51          .has().exactly(sampleValues().e0).inOrder();
52      expectMissing(samples.e0);
53    }
54  
55    @CollectionSize.Require(absent = ZERO)
56    @MapFeature.Require(SUPPORTS_REMOVE)
57    public void testRemoveAllPropagatesToGet() {
58      Collection<V> getResult = multimap().get(sampleKeys().e0);
59  
60      multimap().removeAll(sampleKeys().e0);
61  
62      assertThat(getResult).isEmpty();
63      expectMissing(samples.e0);
64    }
65  
66    @CollectionSize.Require(SEVERAL)
67    @MapFeature.Require(SUPPORTS_REMOVE)
68    public void testRemoveAllMultipleValues() {
69      resetContainer(
70          Helpers.mapEntry(sampleKeys().e0, sampleValues().e0),
71          Helpers.mapEntry(sampleKeys().e0, sampleValues().e1),
72          Helpers.mapEntry(sampleKeys().e0, sampleValues().e2));
73  
74      assertThat(multimap().removeAll(sampleKeys().e0))
75          .has().exactly(sampleValues().e0, sampleValues().e1, sampleValues().e2);
76      assertTrue(multimap().isEmpty());
77    }
78  
79    @CollectionSize.Require(absent = ZERO)
80    @MapFeature.Require({ SUPPORTS_REMOVE, ALLOWS_NULL_KEYS })
81    public void testRemoveAllNullKeyPresent() {
82      initMultimapWithNullKey();
83  
84      assertThat(multimap().removeAll(null)).has().exactly(getValueForNullKey()).inOrder();
85  
86      expectMissing(Helpers.mapEntry((K) null, getValueForNullKey()));
87    }
88  
89    @MapFeature.Require({ SUPPORTS_REMOVE, ALLOWS_ANY_NULL_QUERIES})
90    public void testRemoveAllNullKeyAbsent() {
91      assertThat(multimap().removeAll(null)).isEmpty();
92      expectUnchanged();
93    }
94  }